package com.canbot.u05.activity.vipguide;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.UsherModeRecordDialog;

import java.io.File;
import java.util.HashMap;



/**
 * Created by cwf on 2017/5/25.
 * 贵宾欢迎语设置
 */

public class WelcomeSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private String isSetting;

        private String identification;

        private String vipname;

        private int type;

        private String content;

        private UsherModeRecordDialog usherModeRecordDialog;

        private String fileName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_vip_welcome_setting);

                setViews();

                isSetting = getIntent().getStringExtra("isSetting");
                identification = getIntent().getStringExtra("identification");
                vipname = getIntent().getStringExtra("vipname");
                type=getIntent().getIntExtra("voicetype",0);
                content=getIntent().getStringExtra("content");
                if(!TextUtils.isEmpty(isSetting)){
                     playSound();
                }else{
                        String[] voices = {"u05_schy_001.wav","u052_schy_002.wav"};
                        int index = (int) (Math.random() * voices.length);
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.VIP_GUIDE_DIR + voices[index]);
                }
                Log.e("123", ""+content+""+type+""+isSetting);
        }

        private void setViews() {
                findViewById(R.id.imageView_back).setOnClickListener(this);
                findViewById(R.id.rl_record).setOnClickListener(this);
                findViewById(R.id.rl_text_to_voice).setOnClickListener(this);
                findViewById(R.id.rl_choose_voice).setOnClickListener(this);
        }

        private void playSound() {
                String[] voices = {"u05_gbhh_001.wav", "u05_gbhh_002.wav"};
                int index = (int) (Math.random() * voices.length);
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.VIP_GUIDE_DIR + voices[index]);
                MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "14");
                if (index == 0) {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14001");
                }
                else {
                        MsgSendUtils.sendStringMsg(MsgType.ACTION, "14007");
                }
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.rl_text_to_voice: //文字转语音

                                if(!TextUtils.isEmpty(isSetting)){
                                        startActivity(new Intent(WelcomeSettingActivity.this, VipGuideTextToVoiceActivity.class)
                                                .putExtra("isSetting", isSetting)
                                                .putExtra("identification", identification)//id
                                                .putExtra("name", vipname));
                                }else{
                                        if(1==type){
                                                startActivity(new Intent(WelcomeSettingActivity.this, VipGuideTextToVoiceActivity.class)
                                                        .putExtra("identification", identification)
                                                        .putExtra("content", content));
                                        }else{
                                                startActivity(new Intent(WelcomeSettingActivity.this, VipGuideTextToVoiceActivity.class)
                                                        .putExtra("identification", identification));
                                        }
                                }

                                break;
                        case R.id.rl_choose_voice://选择音频

                                if(!TextUtils.isEmpty(isSetting)){
                                        startActivity(new Intent(WelcomeSettingActivity.this, VipGuideChooseVoiceActivity.class)
                                                .putExtra("isSetting", isSetting)
                                                .putExtra("identification", identification)//id
                                                .putExtra("name", vipname));
                                }else{
                                        if(2==type){
                                                startActivity(new Intent(WelcomeSettingActivity.this, VipGuideChooseVoiceActivity.class)
                                                        .putExtra("identification", identification)
                                                        .putExtra("content",content ));
                                        }else{
                                                startActivity(new Intent(WelcomeSettingActivity.this, VipGuideChooseVoiceActivity.class)
                                                        .putExtra("identification", identification));
                                        }
                                }


                                break;
                        case R.id.rl_record://录音,点击录音直接弹出dialog
                                //点击语音录入图标
                                usherModeRecordDialog = new UsherModeRecordDialog(this, UsherModeRecordDialog.USHER_MODE);
                                usherModeRecordDialog.setFinishClickListener(this);
                                usherModeRecordDialog.setCancleClickListener(this);
                                setDialogWindowAttr(usherModeRecordDialog, this);
                                usherModeRecordDialog.show();
                                break;
                        case R.id.imageView_back:
                                finish();
                                break;
                        case R.id.record_dialog_finish:
                                fileName = usherModeRecordDialog.ok();
                                Logger.e("test", "fileName" + fileName);
                                usherModeRecordDialog.setTime(WelcomeSettingActivity.this.getString(R.string.uploading_please_wait));
                                updataFile();
                                break;
                        case R.id.record_dialog_cancle:
                                usherModeRecordDialog.no();
                                break;

                        default:
                                break;
                }

        }

        public void showHint(String content) {
                HashMap<String, Object> paramsMap = new HashMap<String, Object>();

                //音频
                paramsMap.put("identification", identification);
                paramsMap.put("content", content);
                paramsMap.put("voicetype", 2);
                paramsMap.put("vipType", "updateRoute");

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "routevipGuide", paramsMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(WelcomeSettingActivity.this).showText(getString(R.string.upload_complete));

                                                if (!TextUtils.isEmpty(isSetting)) {
                                                        startActivity(new Intent(WelcomeSettingActivity.this, ReceptionSettingActivity.class)
                                                                .putExtra("isSetting", isSetting)
                                                                .putExtra("identification", identification));
                                                }
                                                else {
                                                        MainApplication.finishActivity(WelcomeSettingActivity.class.getName());
                                                }
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(WelcomeSettingActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }

        private void updataFile() {

                HashMap<String, Object> hasMap = new HashMap<>();

                File file = new File(ConstUtils.PATH_SD +"/record/" ,fileName);
                if (!file.exists()) {
                        file.mkdirs();
                }

                hasMap.put("path", fileName);
                hasMap.put("audiofile", file);

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "uploadRecord", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        showHint(strMsgBean.getResult().toString());

                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(WelcomeSettingActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });

        }

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }



        @Override
        protected void onPause() {
                super.onPause();
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
        }
}
